package cn.cena.pet.service.impl;

import cn.cena.basic.service.impl.BaseServiceImpl;
import cn.cena.basic.util.DistanceUtil;
import cn.cena.basic.util.LoginContextUtil;
import cn.cena.basic.util.PageList;
import cn.cena.basic.util.Point;
import cn.cena.org.domian.Employee;
import cn.cena.org.domian.Shop;
import cn.cena.org.mapper.EmployeeMapper;
import cn.cena.org.mapper.ShopMapper;
import cn.cena.pet.domain.Pet;
import cn.cena.pet.domain.SearchMasterMsg;
import cn.cena.pet.mapper.SearchMasterMsgMapper;
import cn.cena.pet.query.SearchMasterMsgQuery;
import cn.cena.pet.service.IPetDetailService;
import cn.cena.pet.service.IPetService;
import cn.cena.pet.service.ISearchMasterMsgService;
import cn.cena.user.domain.User;
import cn.cena.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IPetService petService;

    /**
     * 寻主发布
     * @param smm
     */
    @Override
    public void publish(SearchMasterMsg smm, Long logininfoId) {
        // 校验： 略
        // 获取用户id
        User user = userMapper.findByLogininfoId(logininfoId);
        smm.setUser_id(user.getId());
        smm.setUser(user);

        // 获取店铺id
        // 当前用户的坐标
        Point point = DistanceUtil.getPoint(smm.getAddress());
        // 查找所有店铺
        List<Shop> shops = shopMapper.findAll();
        // 查找与用户最近的店铺
        Shop shop = DistanceUtil.getNearestShop(point, shops);

        smm.setShop_id(shop.getId());
        smm.setShop(shop);
        searchMasterMsgMapper.save(smm);
    }


    /**
     *  待处理的宠物
     * @param logininfoId
     * @param query
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(Long logininfoId, SearchMasterMsgQuery query) {
        // 根据登录用户信息查找员工信息
        Employee emp = employeeMapper.findByLogininfoId(logininfoId);
        if (emp.getShop_id() != null){ // 员工shop_id不为空表示为店铺员工
            query.setShopId(emp.getShop_id());
        }
        query.setState(0); // 表示未处理的
        PageList<SearchMasterMsg> pageList = super.queryData(query);
        return pageList;
    }


    /**
     * 已处理的宠物
     * @param id
     * @param query
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(Long logininfoId, SearchMasterMsgQuery query) {
        // 根据登录用户信息查找员工信息
        Employee emp = employeeMapper.findByLogininfoId(logininfoId);
        if (emp.getShop_id() != null){ // 员工shop_id不为空表示为店铺员工
            query.setShopId(emp.getShop_id());
        }
        query.setState(1); // 表示已处理的
        PageList<SearchMasterMsg> pageList = super.queryData(query);
        return pageList;
    }

    /**
     * 处理
     * @param pet
     * @param logininfoId
     */
    @Override
    public void handle(Pet pet, Long logininfoId) {

        // 提交后改变当前页面数据状态
        searchMasterMsgMapper.updateById(pet.getSearch_master_msg_id());

        // 添加pet表
        petService.add(pet);



    }


//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
